package com.momo.hr.controller.system.emp;

import com.momo.hr.bean.*;
import com.momo.hr.service.DepartmentService;
import com.momo.hr.service.EmployeeService;
import com.momo.hr.service.NationService;
import com.momo.hr.service.PoliticsStatusService;
import com.momo.hr.service.system.JobLevelService;
import com.momo.hr.service.system.PositionService;
import com.momo.hr.util.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 员工基本资料
 */
@RestController
@RequestMapping("/employee/basic")
public class EmpBasicController {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    JobLevelService jobLevelService;
    @Autowired
    PositionService positionService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    NationService nationService;
    @Autowired
    PoliticsStatusService politicsStatusService;

    /**
     *
     * @param page
     * @param size
     * @param employee
     * @param beginDateScope
     * @return 一个带有分页的结果
     */
    @GetMapping("/")
    public RespPageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer size,
                                          Employee employee, Date[] beginDateScope) {
        return employeeService.getEmployeeByPage(page, size, employee,beginDateScope);
    }

    //添加员工信息
    @PostMapping("/")
    public RespBean addEmployee(@RequestBody Employee employee){

        if(employeeService.addEmp(employee) == 1){
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @GetMapping("/nations")
    public List<Nation> getAllNation(){
        return nationService.getAllNations();
    }

    @GetMapping("/politicsStatus")
    public List<Politicsstatus> getAllPoliticsStatus(){
        return politicsStatusService.getAllPoliticsStatusService();
    }

    @GetMapping("/jobLevels")
    public List<JobLevel> getAllJobLevel(){
        return this.jobLevelService.getAllJobLevel();
    }


    //获取工号
    @GetMapping("/getMaxWorkId")
    public RespBean maxWorkId(){
        //这里采用了链式编程
        return RespBean.build()
                .setStatus(200)
                .setObj(String.format("%08d",this.employeeService.getMaxWorkId() + 1));
    }

    //删除员工
    @DeleteMapping("/{id}")
    public RespBean deleteEmpById(@PathVariable Integer id){
        if(this.employeeService.deleteEmpById(id) == 1){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }

    //更新员工
    @PutMapping("/")
    public RespBean updateEmp(@RequestBody Employee emp){
        if(this.employeeService.updateEmp(emp) == 1){
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }

    //导出数据Excel表格
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData(){
        List data = employeeService.getEmployeeByPage(null, null, null, null).getData();
        return POIUtils.employeeToExcel(data);
    }

    //导入数据 （这里注意 这里的”file“ 名字要和 前端传过来的 的 ”name“属性值的名字要对应，不然会出现null 错误）
    @PostMapping("/import")
    public RespBean importData(MultipartFile file) throws IOException {
        //上传文件的方法
        //file.transferTo(new File("D:\\员工信息表.xls"));
        //把Excel数据导入到数据库中去
        List<Employee> employees = POIUtils.excelToEmployee(file,
                positionService.getAllPosition(), //职位
                nationService.getAllNations(),//籍贯
                politicsStatusService.getAllPoliticsStatusService(),//面貌
                departmentService.getAllDepartmentsWithOutChildren(),//部门
                jobLevelService.getAllJobLevel());//职称

        if(this.employeeService.addEmps(employees) == employees.size()){

            return RespBean.ok("上传成功");
        }
        return RespBean.error("上传失败");
    }


}
